<?php

namespace App\Models\Common;

use Emadadly\LaravelUuid\Uuids;
use WeSoonNet\LaravelPlus\Traits\Timezone;
use App\Services\UrlCast;
use Illuminate\Database\Eloquent\Model;

class File extends Model
{
    use Uuids, Timezone;

    protected $table        = 'com_files';
    protected $guarded      = ['created_at', 'updated_at'];
    protected $hidden       = ['pivot', 'service_id'];
    public    $incrementing = false;
    protected $casts        = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'deleted_at' => 'datetime:Y-m-d H:i:s',
        'path' => UrlCast::class
    ];

    public static function boot()
    {
        parent::boot();

        File::saving(function ($file)
        {
//            $path = public_path($file->path);
//            if (is_file($path))
//            {
//                $pathinfo   = pathinfo($path);
//                $file->type = $pathinfo['extension'];
//                $file->size = round(filesize($path) / 1024, 0);
//            }
        });

        File::deleted(function ($file)
        {
//            if (is_file(public_path($file->path)))
//            {
//                @unlink(public_path($file->path));
//            }
        });
    }
}
